# 导入模块
from apscheduler.schedulers.background import BackgroundScheduler
from wxpy import *

import common.jd_union as jd

bot = None
user_myself = None
logger = logging.getLogger('wx')


def debug():
    bot.file_helper.send('scheduler init!')
    logger.info('test')


# 定时任务初始化
def init_scheduler(bot_var):
    global bot, user_myself
    bot = bot_var
    user_myself = ensure_one(bot.friends().search('kolly'))

    # 后台非阻塞定时任务
    scheduler = BackgroundScheduler()

    # 测试，每 30 秒往文件传输助教发一条消息
    scheduler.add_job(debug, 'interval', seconds=30)  # 间隔执行

    # 京东联盟订单定时查询（每 5 分钟一次）
    scheduler.add_job(get_order, 'cron', year='*', month='*', day='*', day_of_week='*',
                      hour='*', minute='0/5', second='30')

    scheduler.start()


# 查询京东订单
def get_order():
    result = jd.get_order()
    if len(result) > 0:
        user_myself.send('[订单提醒]\n' + result)
